home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group01b.txt / 000047_icon-group-sender_Tue Mar 6 12:31:52 2001.msg < prev    next >
Internet Message Format  |  2002-01-03  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id f26JUmA14033
  4.     for icon-group-addresses; Tue, 6 Mar 2001 12:30:48 -0700 (MST)
  5. Message-Id: <200103061930.f26JUmA14033@baskerville.CS.Arizona.EDU>
  6. Date: Tue, 06 Mar 2001 10:42:10 -0500
  7. From: "Steve Graham" <Steve_Graham@labcorp.com>
  8. To: <icon-group@cs.arizona.edu>, <cary@cup.hp.com>
  9. Subject: Re: New Scientist puzzle
  10. Content-Disposition: inline
  11. X-MIME-Autoconverted: from quoted-printable to 8bit by baskerville.CS.Arizona.EDU id f26Fi7804551
  12. Errors-To: icon-group-errors@cs.arizona.edu
  13. Status: RO
  14. Content-Length: 2852
  15.  
  16. Cary,
  17.  
  18.    This puzzle seems to have taken on a life of its own, especially in the comp.lang.apl forum.  Over 30 posts.
  19.  
  20.    Thanks for both of your submissions.
  21.  
  22.  
  23. Steve
  24.    
  25.  
  26. >>> Cary Coutant <cary@cup.hp.com> 03/05/01 08:20PM >>>
  27. After doing a solution in Icon, I got to thinking that this problem would 
  28. be fun to solve in APL. After dusting off my APL programming skills 
  29. (dormant for 26 years now), I wasn't able to come up with something as 
  30. elegant as I had hoped for. (Some may question the use of "elegant" in 
  31. the same sentence as "APL.")
  32.  
  33. My apologies for going so far off topic, but we've already seen a 
  34. solution in MUMPS (another language I haven't seen for 26 years), and I 
  35. thought this would be another interesting contrast to Icon.
  36.  
  37. What I wanted to do was something like this (with accomodation for the 
  38. ASCII character set):
  39.  
  40. Start with a function F that compares two numbers X and Y and returns a 
  41. boolean indicating whether they match the pattern. The trick in line [2] 
  42. is exactly the same trick I used with map() in my Icon solution.
  43.  
  44.       del R <- X F Y; NUMS
  45. [1]   NUMS <- (((rho PAT1), 0) format X), ((rho PAT2), 0) format Y
  46. [2]   R <- and / (PATS[NUMS iota NUMS]=PATS), NUMS[PATS iota PATS]=NUMS
  47.       del
  48.  
  49. The function VIER takes a vector of squares (line [4]), and forms the 
  50. outer result (line [5]) of that vector with itself, using my function F. 
  51. This would produce a matrix of booleans indicating each pair of squares 
  52. that satisfies the pattern (the first condition). In line [6], I 
  53. plus-reduce the matrix row-wise and column-wise, check for rows and 
  54. columns that have only one solution, expand those reduced vectors out 
  55. again, then and everything together to form a matrix with only a single 
  56. 1. Lines 7 and 8 print the two squares corresponding to that entry.
  57.  
  58.       del VIER; SQ; M
  59. [1]   PAT1 <- 'VIER'
  60. [2]   PAT2 <- 'NEUN'
  61. [3]   PATS <- PAT1,PAT2
  62. [4]   SQ <- (31 drop iota 99) * 2
  63. [5]   M <- SQ jot . F SQ
  64. [6]   M <- M & ((rho M) rho 1 = + bar/ M) &
  65.                transpose (reverse rho M) rho 1 = +/M
  66. [7]   (or / M) / SQ
  67. [8]   (or bar/ M) / SQ
  68.       del
  69.  
  70. Unfortunately, APL doesn't support outer result with user-defined 
  71. functions. (To me, that's like not supporting user-defined generators in 
  72. Icon!) I ended up writing a function XMATCH that produced the outer 
  73. result using traditional looping techniques.
  74.  
  75. Then I had one more problem. The version of APL I was using (CAPLIB for 
  76. DOS, running on a Mac under Virtual PC) crashed when I tried to make the 
  77. 68-by-68 result matrix. I ended up having to filter the list of squares 
  78. into two lists: VSQ containing only those squares that matched the 
  79. pattern "VIER", and NSQ containing only those squares that matched the 
  80. pattern "NEUN". With the smaller matrix to deal with, it was able to come 
  81. up with the solution.
  82.  
  83. It was a fun exercise, though!
  84.  
  85. -cary
  86.  
  87.  
  88.